﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Medianamik.Core;
using Medianamik.Core.Configuration;
using Medianamik.Core.Profile;
using Medianamik.Globalization;
using Telerik.Web.UI;
using Medianamik.Core.Sugar;


namespace Medianamik.UI.Controls
{
    /// <summary>
    /// Contrôle de base pour l'édition dans Médianamik
    /// </summary>
    public abstract class BaseMedianamikControl : CompositeControl, IMedianamikControl
    {
        public static readonly string DefaultValidationGroupName = "B3483B9F573B4E3CB391FDFD069F8776";

        /// <summary>
        /// Validateurs
        /// </summary>
        RequiredFieldValidator reqValidator;
        RegularExpressionValidator regexValidator;
        CustomValidator valIsUnique;
        RegularExpressionValidator maxLengthValidator;

        /// <summary>
        /// Tooltip
        /// </summary>
        Image helpImage;
        RadToolTip helpTooltip;

        //Label
        MedianamikControlLabel label;

        /// <summary>
        /// Message d'erreur par défaut
        /// </summary>
        protected string REQUIRED = Translator.Translate("Medianamik_Controls", "BaseRequiredFieldMessage");
        protected string REGEX = Translator.Translate("Medianamik_Controls", "BaseREGEXMessage");
        protected string UNIQUE = Translator.Translate("Medianamik_Controls", "BaseUniqueMessage");
        protected string INVALID = Translator.Translate("Medianamik_Controls", "BaseInvalidMessage");

        public event ValueChangedEventHandler ValueChanged;
        protected virtual void OnValueChanged(ValueChangedEventArgs e)
        {
            if (ValueChanged != null)
                ValueChanged(this, e);
        }

        /// <summary>
        /// Tailles par défaut
        /// </summary>
        protected Unit WIDTH { get { return 300; } }
        protected Unit HEIGHT { get { return 150; } }

        readonly static MedianamikConfigSection Config = (MedianamikConfigSection)ConfigurationManager
            .GetSection(ConfigurationSectionName.MEDIANAMIK);

        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Div;
            }
        }

        public BasePage MedianamikBasePage
        {
            get { return Page as BasePage; }
        }

        protected virtual WebSite CurrentSite
        {
            get
            {
                if (_checkHasBeenDone == false && _currentWebSite == null)
                {
                    _checkHasBeenDone = true;
                    if (CurrentNode != null)
                    {
                        _currentWebSite = NodeManager.GetParentNodes<WebSite>(CurrentNode, MedianamikTypes.WebSite, true, 0).SingleOrDefault();
                    }
                    else if (ParentNodeId.HasValue && ParentNodeId.Value != Guid.Empty)
                    {
                        _currentWebSite = NodeManager.GetParentNodes<WebSite>(NodeManager.GetNode(ParentNodeId.Value), MedianamikTypes.WebSite, true, 0).SingleOrDefault();
                    }
                }

                return _currentWebSite;
            }
            set
            {
                _checkHasBeenDone = true;
                _currentWebSite = value;
            }
        }
        private WebSite _currentWebSite;
        private bool _checkHasBeenDone;
        protected bool CheckHasBeenDone { get { return _checkHasBeenDone; } }
        protected WebSite CurrentWebSite { get { return _currentWebSite; } }

        protected Node CurrentNode
        {
            get
            {
                if (_currentNode == null)
                    _currentNode = NodeManager.GetNode(NodeId);

                return _currentNode;
            }
        }
        private Node _currentNode;

        protected Node ParentNode
        {
            get
            {
                if (_parentNode == null && ParentNodeId.HasValue)
                    _parentNode = NodeManager.GetNode(ParentNodeId.Value);

                return _parentNode;
            }
        }
        Node _parentNode;

        protected bool IsSyndication
        {
            get
            {
                if (_isSyndication == null)
                {
                    _isSyndication = TypeName.Equals("MedianamikSyndication", StringComparison.OrdinalIgnoreCase);
                }
                return _isSyndication.GetValueOrDefault(false);
            }
        }
        bool? _isSyndication;

        protected NodeType CurrentType
        {
            get
            {
                if (_currentType == null)
                {
                    _currentType = CurrentNode == null
                                 ? NodeTypeManager.GetNodeType(TypeName)
                                 : CurrentNode.NodeType;
                }

                return _currentType;
            }
        }
        NodeType _currentType;

        public const string ControlToValidateId = "B3483B9F573B4E3CB391FDFD069F8785";

        public Control ControlToValidate
        {
            get { return this.FindControlR(ControlToValidateId); }
        }

        public void SetLoadEventHandler(EventHandler e)
        {
            Init += (sender, args) => TrackViewState();
            Init += e;
            Init += e;
        }

        /// <summary>
        /// Référence au contrôle actuel
        /// </summary>
        public Control Control
        {
            get { return this; }
        }

        /// <summary>
        /// Liste des choix pour un control OneToMany ou ManyToMany
        /// </summary>
        public Func<RelationOptionCollection> OptionsDelegate { get; set; }
        private RelationOptionCollection _options;
        public RelationOptionCollection Options
        {
            get
            {
                if (_options == null)
                {
                    _options = OptionsDelegate != null
                             ? OptionsDelegate()
                             : new RelationOptionCollection();
                }
                return _options;
            }
        }

        /// <summary>
        /// Propriété abstraite définissant la valeur du contrôle
        /// </summary>
        public virtual object DataSource { get; set; }

        public abstract object GetValue();

        public virtual void SetValue(object value)
        {
            throw new NotImplementedException("SetValue is not implemented for this control.");
        }

        /// <summary>
        /// Propriété abstraite définissant le type retourné par le contrôle 
        /// </summary>
        public abstract Type TypeOfValue { get; }

        public MedianamikControlInfo ControlInfo
        {
            get
            {
                return (MedianamikControlInfo?)ViewState["ControlInfo"] ?? new MedianamikControlInfo();
            }
            set
            {
                ViewState["ControlInfo"] = value;
            }
        }

        /// <summary>
        /// Expression de validation du contrôle
        /// </summary>
        public virtual string ValidationExpression
        {
            get
            {
                object o = ViewState["ValidationExpression"];
                if (o == null)
                    return String.Empty;
                return (string)o;
            }
            set { ViewState["ValidationExpression"] = value; }
        }

        /// <summary>
        /// Est-ce qu'une valeur est requise par ce contrôle?
        /// </summary>
        public virtual bool IsRequired
        {
            get
            {
                object o = ViewState["IsRequired"];
                if (o == null)
                    return default(bool);
                return (bool)o;
            }
            set { ViewState["IsRequired"] = value; }
        }

        public bool HideNeutralLanguageIndication
        {
            get
            {
                object o = ViewState["HideNeutralLanguageIndication"];
                if (o == null)
                    return default(bool);
                return (bool)o;
            }
            set { ViewState["HideNeutralLanguageIndication"] = value; }
        }

        public bool HideMultiLingualControls
        {
            get
            {
                object o = ViewState["HideMultiLingualControls"];
                if (o == null)
                    return default(bool);
                return (bool)o;
            }
            set { ViewState["HideMultiLingualControls"] = value; }
        }

        /// <summary>
        /// Nom de la propriété Médianamik associée à ce contrôle
        /// </summary>
        public string PropertyName
        {
            get
            {
                object o = ViewState["PropertyName"];
                if (o == null)
                    return String.Empty;
                return (string)o;
            }
            set { ViewState["PropertyName"] = value; }
        }

        public string ValidationGroupName
        {
            get
            {
                return ViewState["ValidationGroupName"] == null ? DefaultValidationGroupName :
                    ViewState["ValidationGroupName"].ToString();
            }
            set { ViewState["ValidationGroupName"] = value; }
        }

        /// <summary>
        /// Type de la propriété associée à ce contrôle
        /// </summary>
        public string TypeName
        {
            get
            {
                return ViewState["TypeName"] as string ?? string.Empty;

            }
            set { ViewState["TypeName"] = value; }
        }

        public string EditionCultureName
        {
            get
            {
                return ViewState["EditionCultureName"] as string
                       ?? (string)(ViewState["EditionCultureName"] = MedianamikContext.Current.EditionCulture.Name);
            }
            set { ViewState["EditionCultureName"] = value; }
        }

        /// <summary>
        /// Est-ce que la valeur de ce contrôle doit être unique?
        /// </summary>
        public virtual bool IsUnique
        {
            get
            {
                object o = ViewState["IsUnique"];
                if (o == null)
                    return default(bool);
                return (bool)o;
            }
            set { ViewState["IsUnique"] = value; }
        }

        public virtual int? MaxLength
        {
            get
            {
                return (int?)ViewState["MaxLength"];
            }
            set
            {
                ViewState["MaxLength"] = value;
            }
        }

        public virtual int? MinLength
        {
            get
            {
                return (int?)ViewState["MinLength"];
            }
            set
            {
                ViewState["MinLength"] = value;
            }
        }

        /// <summary>
        /// Noeud de Médianamik auquel ce contrôle est associé
        /// </summary>
        public virtual Guid NodeId
        {
            get
            {
                object o = ViewState["NodeId"];
                if (o == null)
                    return Guid.Empty;
                return (Guid)o;
            }
            set { ViewState["NodeId"] = value; }
        }

        /// <summary>
        /// Noeud parent d'un noeud de Médianamik auquel ce contrôle est associé
        /// </summary>
        public Guid? ParentNodeId
        {
            get
            {
                object o = ViewState["ParentNodeId"];
                if (o == null)
                    return null;
                return (Guid)o;
            }
            set { if (value != null) { ViewState["ParentNodeId"] = value; } }
        }

        /// <summary>
        /// Instance du noeud de Médianamik auquel ce contrôle est associé
        /// </summary>
        public Guid NodeInstanceId
        {
            get
            {
                object o = ViewState["NodeInstanceId"];
                if (o == null)
                    return Guid.Empty;
                return (Guid)o;
            }
            set { ViewState["NodeInstanceId"] = value; }
        }

        /// <summary>
        /// Type Médianamik associé
        /// </summary>
        public Guid NodeTypePropertyId
        {
            get
            {
                object o = ViewState["NodeTypePropertyId"];
                if (o == null)
                    return Guid.Empty;
                return (Guid)o;
            }
            set { ViewState["NodeTypePropertyId"] = value; }
        }

        /// <summary>
        /// Type Médianamik associé
        /// </summary>
        public Guid NodeTypeId
        {
            get
            {
                object o = ViewState["NodeTypeId"];
                if (o == null)
                    return Guid.Empty;
                return (Guid)o;
            }
            set { ViewState["NodeTypeId"] = value; }
        }

        /// <summary>
        /// Faut-il générer automatiquement les validateurs?
        /// </summary>
        public virtual bool AutoGenerateValidators
        {
            get
            {
                object o = ViewState["AutoGenerateValidators"];
                if (o == null)
                    return default(bool);
                return (bool)o;
            }
            set { ViewState["AutoGenerateValidators"] = value; }
        }

        public bool EnableClientScriptValidation
        {
            get
            {
                object o = ViewState["EnableClientScriptValidation"];
                if (o == null)
                {
                    return false;
                }
                return (bool)o;
            }
            set { ViewState["EnableClientScriptValidation"] = value; }
        }

        protected virtual bool SpecialLabel
        {
            get { return false; }
        }

        /// <summary>
        /// Étiquette associée à ce contrôle
        /// </summary>
        public bool DisplayLabel
        {
            get
            {
                if (SpecialLabel)
                    return false;

                return ViewState["DisplayLabel"] as bool? ?? true;
            }
            set { ViewState["DisplayLabel"] = value; }
        }

        /// <summary>
        /// ???
        /// </summary>
        public string Label
        {
            get
            {
                object o = ViewState["Label"];
                if (o == null || o.ToString().Length == 0)
                    return Translator.Translate("TypeManagement", NodeTypePropertyId + "_Label") + LabelLanguageExtensionText;
                return (string)o;
            }
            set { ViewState["Label"] = value; }
        }

        /// <summary>
        /// ???
        /// </summary>
        public string LabelExtensionText
        {
            get
            {
                object o = ViewState["LabelExtensionText"];
                if (o == null)
                    return default(String);
                return (string)o;
            }
            set { ViewState["LabelExtensionText"] = value; }
        }

        public string LabelLanguageExtensionText
        {
            get
            {
                object o = ViewState["LabelLanguageExtensionText"];
                if (o == null)
                    return default(String);
                return (string)o;
            }
            set { ViewState["LabelLanguageExtensionText"] = value; }
        }

        public string TabName
        {
            get
            {
                object o = ViewState["TabName"];
                if (o == null)
                    return default(String);
                return (string)o;
            }
            set { ViewState["TabName"] = value; }
        }

        /// <summary>
        /// Est-ce que ce contrôle possède une bulle d'aide?
        /// </summary>
        public bool HaveTooltip
        {
            get
            {
                object o = ViewState["HaveTooltip"];
                if (o == null)
                    return false;
                return (bool)o;
            }
            set { ViewState["HaveTooltip"] = value; }
        }

        /// <summary>
        /// Est-ce que le contrôle permet la saisie multilingue ?
        /// </summary>
        public bool IsMultiLingual
        {
            get { return (bool?)ViewState["IsMultilingual"] ?? false; }

            set
            {
                ViewState["IsMultilingual"] = value;
            }
        }

        protected bool DataBound
        {
            get
            {
                return (bool?)ViewState["DataBound"] ?? false;
            }
            set
            {
                ViewState["DataBound"] = value;
            }
        }

        /// <summary>
        /// TODO: Revoir
        /// Hack pour ne pas rajouter le &lt;p&gt; autour du contenu du controle
        /// Utilisé pour le ImagePicker
        /// </summary>
        protected virtual bool WrapContentInParagraph
        {
            get
            {
                return true;
            }
        }

        public override Unit Width
        {
            get
            {
                if (SizedControl == null)
                    return base.Width;

                return SizedControl.Width;
            }
            set
            {
                if (SizedControl == null)
                    throw new ApplicationException("The SizedControl is not specified.");

                SizedControl.Width = value;
            }
        }

        public override Unit Height
        {
            get
            {
                if (SizedControl == null)
                    return base.Height;

                return SizedControl.Height;
            }
            set
            {
                if (SizedControl == null)
                    throw new ApplicationException("The SizedControl is not specified.");

                SizedControl.Height = value;
            }
        }

        protected virtual WebControl SizedControl
        {
            get { return null; }
        }

        public bool IsLanguageNeutral
        {
            get
            {
                return ViewState["IsLanguageNeutral"] as bool? ?? false;
            }
            set
            {
                ViewState["IsLanguageNeutral"] = value;
            }
        }

        public override void DataBind()
        {
            base.DataBind();
            DataBound = true;
        }

        public virtual bool IsInsideMedianamik
        {
            get { return Page as BasePage != null; }
        }

        private MedianamikFormTemplate _medianamikForm;
        public MedianamikFormTemplate MedianamikForm
        {
            get
            {
                if (_medianamikForm == null)
                {
                    Control parent = this;

                    while (parent != null)
                    {
                        if (parent.GetType() == typeof(MedianamikFormTemplate))
                            return (MedianamikFormTemplate)parent;

                        parent = parent.Parent;
                    }
                }

                return _medianamikForm;
            }
        }

        /// <summary>
        /// Création de la hierarchie de contrôles
        /// </summary>
        protected override void CreateChildControls()
        {
            if (AutoGenerateValidators && MaxLength.HasValue)
            {
                maxLengthValidator = new RegularExpressionValidator();
                Controls.Add(maxLengthValidator);
            }

            if (AutoGenerateValidators && IsRequired)
            {
                reqValidator = new RequiredFieldValidator();
                Controls.Add(reqValidator);
            }

            if (AutoGenerateValidators && ValidationExpression.Length > 0)
            {
                regexValidator = new RegularExpressionValidator();
                Controls.Add(regexValidator);
            }

            if (AutoGenerateValidators && IsUnique)
            {
                valIsUnique = new CustomValidator();
                valIsUnique.ServerValidate += ServerValidateUnique;
                Controls.Add(valIsUnique);
            }

            if (DisplayLabel)
            {
                label = new MedianamikControlLabel();
                Controls.Add(label);
                label.Text = Label + LabelExtensionText.PrefixIfNotEmpty("&nbsp;");
                label.IsRequired = IsRequired;
                label.IsLanguageNeutral = IsLanguageNeutral && !HideNeutralLanguageIndication;

                if (HaveTooltip)
                {
                    if (IsInsideMedianamik)
                    {
                        helpImage = new Image { ID = "AC44BCADF5B244AAA7173183B9DE1086" };
                        helpTooltip = new RadToolTip { ID = "6842B428071E4209BFBC64AACEF611B9" };
                        Controls.Add(helpImage);
                        Controls.Add(helpTooltip);
                    }
                    else
                    {
                        //label.MedianamikToolTip = Translator.Translate("Medianamik_Controls", "MaxLengthRightPart");
                        label.MedianamikToolTip =
                            Translator.Translate("TypeManagement",
                            NodeTypePropertyId + "_Tooltip") + "&nbsp;&nbsp;";
                    }
                }
            }

            if (IsInsideMedianamik)
                Controls.Add(new Literal { Text = "<div class=\"EditionControl\">" });

            CreateControlHierarchy(!DataBound);

            if (DisplayLabel)
            {
                var control = FindControl(ControlToValidateId);

                if (control != null)
                {
                    label.ForControlId = control.ClientID;
                }
            }

            if (IsMultiLingual && !HideMultiLingualControls && CurrentNode != null &&
                !CurrentNode.Instances[0].Properties[PropertyName].IsLanguageNeutral)
            {
                var culurePanel = new Panel();
                var allCultures = ConfigManager.GetEditionCultures().Where(t => t.CultureName != MedianamikContext.Current.EditionCulture.Name);
                allCultures.ForEach(cul => CreateMultilingualControl(cul, culurePanel, CurrentNode, PropertyName));
                Controls.Add(culurePanel);
            }

            if (IsInsideMedianamik)
                Controls.Add(new Literal { Text = "</div>", Visible = Visible });

            if (!DataBound)
            {
                if (AutoGenerateValidators && MaxLength.HasValue)
                {
                    maxLengthValidator.Display = ValidatorDisplay.None;
                    maxLengthValidator.ErrorMessage = Label + " " +
                      Translator.Translate("Medianamik_Controls", "ContentLengthExceedsMaxLength") +
                      String.Format(" ({0} {1})", MaxLength,
                      Translator.Translate("Medianamik_Controls", "MaxLengthRightPart"));

                    if (!String.IsNullOrEmpty(TabName))
                        maxLengthValidator.ErrorMessage +=
                            Translator.Translate("Medianamik_ContentManagement", "SeeTab") +
                            Translator.Translate(ResourceSets.Tabs, TabName.ToLower());

                    maxLengthValidator.ControlToValidate = ControlToValidateId;
                    maxLengthValidator.EnableClientScript = EnableClientScriptValidation;
                    maxLengthValidator.ValidationExpression = @"^(.|\n){0," + MaxLength.Value + "}$";
                    maxLengthValidator.ValidationGroup = ValidationGroupName;
                    maxLengthValidator.Visible = Visible;
                }

                if (AutoGenerateValidators && IsRequired)
                {
                    reqValidator.Display = ValidatorDisplay.None;
                    reqValidator.ControlToValidate = ControlToValidateId;
                    reqValidator.ErrorMessage = (Label + " " + REQUIRED);
                    reqValidator.EnableClientScript = EnableClientScriptValidation;
                    reqValidator.ValidationGroup = ValidationGroupName;
                    reqValidator.Visible = Visible;

                    if (!String.IsNullOrEmpty(TabName))
                        reqValidator.ErrorMessage +=
                            Translator.Translate("Medianamik_ContentManagement", "SeeTab") +
                            Translator.Translate(ResourceSets.Tabs, TabName.ToLower());
                }

                if (AutoGenerateValidators && ValidationExpression.Length > 0)
                {
                    regexValidator.Display = ValidatorDisplay.None;
                    regexValidator.ControlToValidate = ControlToValidateId;
                    regexValidator.ValidationExpression = ValidationExpression;
                    regexValidator.Display = ValidatorDisplay.None;
                    regexValidator.ErrorMessage = Label + " " + REGEX;
                    regexValidator.EnableClientScript = EnableClientScriptValidation;
                    regexValidator.ValidationGroup = ValidationGroupName;
                    regexValidator.Visible = Visible;

                    if (!String.IsNullOrEmpty(TabName))
                        regexValidator.ErrorMessage +=
                            Translator.Translate("Medianamik_ContentManagement", "SeeTab") +
                            Translator.Translate(ResourceSets.Tabs, TabName.ToLower());
                }

                if (AutoGenerateValidators && IsUnique)
                {
                    valIsUnique.Display = ValidatorDisplay.None;
                    valIsUnique.ControlToValidate = ControlToValidateId;
                    valIsUnique.ErrorMessage = Label + " " + UNIQUE;
                    valIsUnique.EnableClientScript = false;
                    valIsUnique.ValidationGroup = ValidationGroupName;
                    valIsUnique.Visible = Visible;

                    if (!String.IsNullOrEmpty(TabName))
                        valIsUnique.ErrorMessage +=
                            Translator.Translate("Medianamik_ContentManagement", "SeeTab") +
                            Translator.Translate(ResourceSets.Tabs, TabName.ToLower());
                }

                if (IsInsideMedianamik && HaveTooltip)
                {
                    helpImage.ImageUrl = "/medianamik/images/icones/ico_aide.gif";
                    helpTooltip.TargetControlID = helpImage.ID;
                    helpTooltip.RelativeTo = ToolTipRelativeDisplay.Element;
                    helpTooltip.Position = ToolTipPosition.MiddleRight;
                    helpTooltip.ShowEvent = ToolTipShowEvent.OnMouseOver;
                    helpTooltip.Text = Translator.Translate("TypeManagement", NodeTypePropertyId + "_Tooltip") + "&nbsp;&nbsp;";
                    helpImage.Visible = Visible;
                }
            }
        }

        /// <summary>
        /// Créer le contrôle de copie des contenus vers d'autres langues
        /// </summary>
        /// <param name="cult">Culture à créer</param>
        /// <param name="parentControl">Conteneur parent</param>
        /// <param name="nd">Noeud actuel</param>
        /// <param name="propertyName">Nom de la propriété visée</param>
        public void CreateMultilingualControl(Culture cult, Control parentControl,
            Node nd, string propertyName)
        {
            if (!nd.NodeType.IsSystem && Config.EnableMultiLanguageFeatures &&
                ((UserProfile)HttpContext.Current.Profile).EnableMultiLanguageFeatures &&
                nd.Instances.Exists(cult.CultureName))
            {
                var chkCulture = new CheckBox { ID = "Culture_" + cult.CultureName, Visible = Visible };

                var lnk = new HyperLink { Text = cult.CultureName, Visible = Visible };

                string cssFilePath;
                if (nd.WebSite != null && !String.IsNullOrEmpty(nd.WebSite.CSSFilePath))
                    cssFilePath = nd.WebSite.CSSFilePath;
                else
                    cssFilePath = Config.DefaultEditorCSSFilePath;

                var propertyId = nd.Instances[cult.CultureName].Properties[PropertyName].NodeTypePropertyID;
                var url = "template.tpl".AddParam("css", cssFilePath).AddParam("propertyid", propertyId)
                    .AddParam("nodeid", nd.NodeId).AddParam("culture", cult.CultureName);

                lnk.NavigateUrl = String.Format("javascript:void(radopen('{0}', 'PropertyPreviewWindow'));", url);

                parentControl.Controls.Add(chkCulture);
                parentControl.Controls.Add(new LiteralControl("&nbsp;") { Visible = Visible });
                parentControl.Controls.Add(lnk);
                parentControl.Controls.Add(new LiteralControl("&nbsp;&nbsp;") { Visible = Visible });
            }
        }

        /// <summary>
        /// Méthode abstraite devant définir la hierarchie des contrôles
        /// </summary>
        protected abstract void CreateControlHierarchy(bool pUseDataSource);

        /// <summary>
        /// Validation unique côté serveur
        /// </summary>
        /// <param name="source"></param>
        /// <param name="args"></param>
        protected virtual void ServerValidateUnique(object source, ServerValidateEventArgs args)
        {
            args.IsValid = NodeManager.PropertyValueIsUnique(NodeId, NodeTypeId, NodeTypePropertyId, GetValue());
        }

        public List<Culture> GetCultureTargets()
        {
            var targets = new List<Culture>();

            var allCultures = ConfigManager.GetEditionCultures().Where(t => t.CultureName != MedianamikContext.Current.EditionCulture.Name);
            foreach (Culture cult in allCultures)
            {
                var chkCulture = (CheckBox)Control.FindControl("Culture_" + cult.CultureName);
                if (chkCulture != null)
                    if (chkCulture.Checked)
                        targets.Add(cult);
            }

            return targets;
        }

        //protected override void AddAttributesToRender(HtmlTextWriter writer)
        //{
        //    base.AddAttributesToRender(writer);

        //    if (!IsInsideMedianamik && CssClass != "")
        //    {
        //        writer.AddAttribute("class", CssClass);
        //    }
        //}
    }

    public class ValueChangedEventArgs : EventArgs
    {
        private readonly object _value;

        public ValueChangedEventArgs(object value)
        {
            _value = value;
        }

        public object Value
        {
            get
            {
                return _value;
            }
        }
    }

    public delegate void ValueChangedEventHandler(object sender, ValueChangedEventArgs e);
}